﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using DecodeInterface;

namespace VirtualVisionSpace
{
    public partial class Form1 : Form
    {
        //Rectangle rectVision = new Rectangle(302, 238, 100, 100);
        //Rectangle rectVision = new Rectangle(100, 100, 300, 300);

        IVision vision = new VisionProvider();
        IEnvironment environment = new RandomEnvironment(new Size(704, 576));
        DecodeExplorer decodeExplorer;
        public Form1()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                    ControlStyles.OptimizedDoubleBuffer |
                    ControlStyles.UserPaint, true);
            InitializeComponent();

            vision.EnvironmentCallback = environment.GetEnvironment;
            vision.VisionCallback = ShowVision;
            vision.VisionRegion = new Rectangle(0, 0, 704, 576);
            decodeExplorer = new DecodeExplorer(decodeShow1);
            vision.Start();
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        void ShowVision(Bitmap image)
        {
            decodeExplorer.InputImage(image);
            decodeExplorer.ShowImage();
            SetWindowTextCall(string.Format("实时 {0:D3} 平均 {1:D3}", this.vision.RealEvnironmentElapsed, this.vision.AvgEvnironmentElapsed));
        }

        delegate void SetWindowTextCallback(string text);
        //在标题栏上显示相关参数
        void SetWindowTextCall(string text)
        {
            try
            {
                if (this.InvokeRequired)
                    this.Invoke(new SetWindowTextCallback(SetWindowTextCall), new object[] { text });
                else
                    this.Text = text;
            }
            catch
            {

            }
        }
    }
}
